package io.renren.modules.eightapp.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.eightapp.dto.EiParkInfoDTO;
import io.renren.modules.eightapp.dto.ExcelDTO;
import io.renren.modules.eightapp.excel.EiParkInfoExcel;
import io.renren.modules.eightapp.service.EiParkInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * 
 *
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2022-12-28
 */
@RestController
@RequestMapping("eightapp/eiparkinfo")
@Api(tags="")
public class EiParkInfoController {
    @Autowired
    private EiParkInfoService eiParkInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("eightapp:eiparkinfo:page")
    public Result<PageData<EiParkInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){

        PageData<EiParkInfoDTO> page = eiParkInfoService.page(params);

        return new Result<PageData<EiParkInfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("eightapp:eiparkinfo:info")
    public Result<EiParkInfoDTO> get(@PathVariable("id") Long id){
        EiParkInfoDTO data = eiParkInfoService.get(id);

        return new Result<EiParkInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("eightapp:eiparkinfo:save")
    public Result save(@RequestBody EiParkInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        eiParkInfoService.savePark(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("eightapp:eiparkinfo:update")
    public Result update(@RequestBody EiParkInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        eiParkInfoService.updatePark(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("eightapp:eiparkinfo:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        eiParkInfoService.deletePark(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("eightapp:eiparkinfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<EiParkInfoDTO> list = eiParkInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, EiParkInfoExcel.class);
    }

    @PostMapping("upload1")
    @ApiOperation("上传")
    @LogOperation("上传")
    public Result uploadFile(@RequestParam("file") MultipartFile multipartFile) {
        try {
            // 保证文件名的唯一，截取文件名后缀 例如：aaa.jpg
            String originalFilename = multipartFile.getOriginalFilename();
            // 截取文件名后缀 .jpg
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 生成唯一文件名 拼接成为 dadesbawe-d5aa64.jpg
            String newFile = UUID.randomUUID() + substring;
            // 指定上传目录--->时间目录
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            // 2022-02-16 日期目录
//            String serverPath = new File("").getCanonicalPath() + File.separator + "files";
            String serverPath = System.getProperty("user.dir") + File.separator + "files" + File.separator + "park";
            // E://temp/${dir}/2022/02/16
            File targetFile = new File(serverPath);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            // 将文件上传到指定目录
            File targetFileName = new File(targetFile, newFile);
            //最终目录: E://temp/${dir}//2022/02/16/dadesbawe-d5aa64.jpg

            multipartFile.transferTo(targetFileName);
            // 将用户选择的aaa.jpg 上传到E://temp/${dir}//2022/02/16/dadesbawe-d5aa64.jpg
            return new Result().ok(newFile);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("上传失败");
        }
    }

    @DeleteMapping("parkdelete")
    @ApiOperation("删除结构")
    @LogOperation("删除结构")
    public Result deleteFile(@RequestParam("fileInfo") String fileInfo) throws IOException {
        String suffix = System.getProperty("user.dir") + File.separator + "files" + File.separator + "park" + File.separator;
        System.out.println(fileInfo);
        String filename = suffix + fileInfo;
        File file = new File(filename);
        System.out.println(file);
        if (file.exists()) {
            boolean deleteRight=file.delete();
            if (deleteRight) {
                return new Result().ok("删除成功");
            }
        }
        return new Result().error("文件不存在");
    }
}